home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1994 November / macformat-018.iso / Utility Spectacular / AfterDark / TwilightZone ƒ / source / utils.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-06-14  |  3.8 KB  |  236 lines  |  [TEXT/KAHL]

  1. /*-------------------------------------------------------------------------------------
  2.  *
  3.  * Simple Sample Application Framework
  4.  *
  5.  * ©1991 Apple Computer
  6.  *
  7.  -------------------------------------------------------------------------------------*/
  8. /*
  9.  * utils.c -- utility routines
  10.  *
  11.  * change history:
  12.  *
  13.  * SJF        11/6/91        1.0d1        initial coding
  14.  *
  15.  */
  16.  
  17. #include <GestaltEqu.h>
  18. #include <Traps.h>
  19. #include <Notification.h>
  20.  
  21. #include "const.h"
  22. #include "globals.h"
  23. #include "trapavailable.h"
  24. #include "main.h"
  25. #include "mymenus.h"
  26.  
  27. #include "utils.h"
  28.  
  29. Boolean HasColorQD(void)
  30. {
  31.     SysEnvRec theEnv;
  32.     
  33.     if (SysEnvirons(1,&theEnv) != noErr)
  34.         return false;
  35.  
  36.     return (theEnv.hasColorQD);
  37. }
  38.  
  39.  
  40. void DoError(OSErr err)
  41. {
  42.     Str255 errStr;
  43.     Str255 errNumStr;
  44.     
  45.     if (err==noErr)
  46.         return;
  47.     
  48.     NumToString(err,errNumStr);
  49.     pstrcpy(errStr,"\pAn error has occurred: ");
  50.     pstrcat(errStr,errNumStr);
  51.     
  52.     Notify(errStr);
  53. //    DebugStr(errStr);
  54. }
  55.  
  56.  
  57. void Notify(StringPtr string)
  58. {
  59.     NMRecPtr nm;
  60.     StringPtr strPtr;
  61.     
  62.     nm = (NMRecPtr)NewPtr(sizeof(NMRec));
  63.     if (MemError()!=noErr)
  64.         return;
  65.     strPtr = (StringPtr)NewPtr(string[0]);
  66.     if (MemError()!=noErr)
  67.         return;
  68.     BlockMove(string,strPtr,string[0]+1);
  69.     
  70.     nm->qType = nmType;
  71.     nm->nmMark = 0;
  72.     nm->nmIcon = nil;
  73.     nm->nmSound = nil;
  74.     nm->nmStr = strPtr;
  75.     nm->nmResp = nil;
  76.     NMInstall(nm);
  77. }
  78.  
  79.  
  80. void pstrcpy(void *dest,void *src)
  81. {
  82.     unsigned char srcLen = ((unsigned char *)src)[0];
  83.     
  84.     BlockMove(src,dest,srcLen+1);
  85. }
  86.  
  87.  
  88. void pstrcat(void *original,void *catStr)
  89. {
  90.     short length;
  91.     unsigned char originalLen = ((unsigned char *)original)[0];
  92.     unsigned char catStrLen = ((unsigned char *)catStr)[0];
  93.     
  94.     length = (short) originalLen;
  95.     length += (short) catStrLen;
  96.     
  97.     if (length > 255) {
  98.         DebugStr("\pstring catenation overflow");
  99.         ExitProc();
  100.     }
  101.     
  102.     BlockMove((char *)catStr+1,(char *)original+originalLen+1,catStrLen);
  103.     ((unsigned char *)original)[0] = (unsigned char) length;
  104. }
  105.  
  106.  
  107. void *NewPtrChk(Size ptrSize)
  108. {
  109.     Ptr thePtr;
  110.  
  111.     thePtr = NewPtr(ptrSize);
  112.     if (MemError()!=noErr)
  113.         DoError(MemError());
  114. #if kDEBUG
  115.     {
  116.         long *longPtr = (long *)thePtr;
  117.         *longPtr = kBetterBusErr;
  118.     }
  119. #endif
  120.     return thePtr;
  121. }
  122.  
  123.  
  124. void *NewHandleChk(Size hndlSize)
  125. {
  126.     Handle theHndl;
  127.     
  128.     theHndl = NewHandle(hndlSize);
  129.     if (MemError()!=noErr)
  130.         DoError(MemError());
  131. #if kDEBUG
  132.     {
  133.         long **longHndl = (long **)theHndl;
  134.         **longHndl = kBetterBusErr;
  135.     }
  136. #endif
  137.     return theHndl;
  138. }
  139.  
  140.  
  141. void DisposPtrChk(void *thePtr)
  142. {
  143. #if kDEBUG
  144.     {
  145.         long *longPtr = (long *)thePtr;
  146.         *longPtr = kBetterBusErr;
  147.     }
  148. #endif
  149.  
  150.     DisposPtr(thePtr);
  151.     if (MemError()!=noErr)
  152.         DoError(MemError());
  153. }
  154.  
  155.  
  156. void DisposHandleChk(void *theHndl)
  157. {
  158. #if kDEBUG
  159.     {
  160.         long **longHndl = (long **)theHndl;
  161.         **longHndl = kBetterBusErr;
  162.     }
  163. #endif
  164.  
  165.     DisposHandle(theHndl);
  166.     if (MemError()!=noErr)
  167.         DoError(MemError());
  168. }
  169.  
  170.  
  171. OSErr WaitPBDone(void *voidBlock)
  172. {
  173.     IOParam *pBlock;
  174.     
  175.     pBlock = (IOParam *)voidBlock;
  176.     while (pBlock->ioResult > 0)
  177.         Yield(0);
  178.     return pBlock->ioResult;
  179. }
  180.  
  181.  
  182. void Yield(long sleepTime)
  183. {
  184.     EventRecord ev;
  185.     
  186.     EventAvail(everyEvent,&ev);
  187. }
  188.  
  189.  
  190. void ExitProc(void)
  191. {
  192.     ExitToShell();
  193. }
  194.  
  195.  
  196. void DisableAllMenus(void)
  197. {
  198.     MenuHandle theMenu;
  199.     short menuIndex,itemIndex,numItems;
  200.     
  201.     for (menuIndex=kAppleMenu+1; menuIndex<kAppleMenu+kNumMenus; menuIndex++) {
  202.         theMenu = GetMHandle(menuIndex);
  203.         numItems = CountMItems(theMenu);
  204.         for (itemIndex=1; itemIndex<=numItems; itemIndex++)
  205.             DisableItem(theMenu,itemIndex);
  206.     }
  207.     
  208.     DrawMenuBar();
  209. }
  210.  
  211.  
  212. void EnableAllMenuItems(MenuHandle theMenu)
  213. {
  214.     short itemIndex,numItems;
  215.     
  216.     numItems = CountMItems(theMenu);
  217.     for (itemIndex=1; itemIndex<=numItems; itemIndex++)
  218.         EnableItem(theMenu,itemIndex);
  219. }
  220.  
  221.  
  222. void SetDefaultMenus(void)
  223. {
  224.     MenuHandle theMenu;
  225.     
  226.     DisableAllMenus();
  227.     theMenu = GetMHandle(kFileMenu);
  228.     EnableItem(theMenu,kOpenItem);
  229.     EnableItem(theMenu,kQuitItem);
  230.     
  231.     theMenu = GetMHandle(kEditMenu);
  232.     EnableItem(theMenu,kCutItem);
  233.     EnableItem(theMenu,kCopyItem);
  234.     EnableItem(theMenu,kPasteItem);
  235.     EnableItem(theMenu,kClearItem);
  236. }